package wec_wallet.ebo.com.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.graphics.Rect;
import android.net.http.SslError;
import android.view.inputmethod.InputMethodManager;
import android.webkit.SslErrorHandler;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import bassproject.ebo.com.ebobass.application.MyApplication;
import bassproject.ebo.com.ebobass.http_utils.interfaces.IHttpPostListener;
import bassproject.ebo.com.ebobass.http_utils.utils.Ipost;
import bassproject.ebo.com.ebobass.utils.I_Share;
import bassproject.ebo.com.ebobass.utils.Lg;
import bassproject.ebo.com.ebobass.utils.StringUtil;
import wec_wallet.ebo.com.R;
import wec_wallet.ebo.com.activity.main.MainActivity;
import wec_wallet.ebo.com.activity.register.LoginActivity;

/**
 * Created by admin on 2018/3/12.
 */

public class Functions {
//aa
    public static void loadWeb(WebView web, String url) {
        Lg.d("loadWeb url : " + url);
        web.getSettings().setUseWideViewPort(true);
        web.getSettings().setLoadWithOverviewMode(true);
        web.setWebViewClient(new WebViewClient() {
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                // 　　//handler.cancel(); 默认的处理方式，WebView变成空白页
                handler.proceed(); // 接受证书
                // handleMessage(Message msg); 其他处理
            }
        });
        // 这两行代码一定加上否则效果不会出现
        web.getSettings().setJavaScriptEnabled(true);
        web.getSettings().setDefaultTextEncodingName("gb2312");


        //隐藏缩放工具
        web.getSettings().setDisplayZoomControls(false);
        // 设置可以支持缩放
        web.getSettings().setSupportZoom(true);
        // 设置出现缩放工具
        web.getSettings().setBuiltInZoomControls(true);
        //扩大比例的缩放
        web.getSettings().setUseWideViewPort(true);
        //自适应屏幕
        web.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        web.getSettings().setLoadWithOverviewMode(true);
//        webView.setInitialScale(10);

        web.loadUrl(url);

    }


    //获取手机区号接口
    public static void getPhonePrefix(Activity activity, IHttpPostListener listener) {
//        Map<String, Object> postParams;
//        postParams = new HashMap<String, Object>();
//        postParams.put("lan", I_Share.getLanguageType_String());
        Functions.post(activity, URLs.GET_PHONE_PREFIX, null, listener, true, true);
    }


    public static Float BigDecimal8(String currentLat) {
        BigDecimal b = new BigDecimal(currentLat);

        return b.setScale(8, BigDecimal.ROUND_HALF_UP).floatValue();
    }

    /**
     * 关闭软键盘
     */
    public static void closeKeybord(EditText mEditText, Context mContext) {
        InputMethodManager imm = (InputMethodManager) mContext
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0);
    }

    //时间戳
    public static String getDate() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String date = df.format(new Date());// new Date()为获取当前系统时间，也可使用当前时间戳
        return date;
    }

    public static String getTime() {
        long time = System.currentTimeMillis() / 1000;
        return "" + time;
    }

    //判断date_old是否超过了date_new一天以上
    public static boolean isNextDay(String date_old, String date_new) {
        if (StringUtil.isEmpty(date_old) || StringUtil.isEmpty(date_new)) {
            //如果为空，则判断为一天以上
            return true;
        }
//        Lg.p("date_old="+date_old+" date_new="+date_new);
        int year_old = Integer.parseInt(date_old.substring(0, 4));
        int year_new = Integer.parseInt(date_new.substring(0, 4));
        int month_old = Integer.parseInt(date_old.substring(5, 7));
        int month_new = Integer.parseInt(date_new.substring(5, 7));
        int day_old = Integer.parseInt(date_old.substring(8, 10));
        int day_new = Integer.parseInt(date_new.substring(8, 10));
//        Lg.d("year_old = " + year_old + " year_new=" + year_new + " month_old=" + month_old + " month_new=" + month_new + " day_old=" + day_old + " day_new=" + day_new);
        if (year_new > year_old) {
            //年比较
            return true;
        } else if (year_new == year_old) {
            //月比较
            if (month_new > month_old) {
                return true;
            } else if (month_new == month_old) {
                //天比较
                if (day_new > day_old) {
                    return true;
                }
            }
        }
        return false;
    }

    //获取整个字符串宽度
    public static int getStringW(Paint pFont, String str) {
        Rect rect = new Rect();
        pFont.getTextBounds(str, 0, str.length(), rect);
        return rect.width();
    }

    //获取字符串高度
    public static int getStringH(Paint pFont, String str) {

        Rect rect = new Rect();
        pFont.getTextBounds(str, 0, 1, rect);
//        Log.v(TAG, "height:"+rect.height()+"width:"+rect.width());

        return rect.height();
    }

    //科学计数法转普通计数法
    public static String getOrdinaryCounting(String str) {
        BigDecimal db = new BigDecimal(str);
        str = db.toPlainString();
        return str;
    }


    //重新登录
    public static void relogin(Context context) {
        Lg.d("===============relogin==============");
//        IToast.show(context.getResources().getString(R.string.token_outdate));
        I_Share.setIsLogin(false);
        Intent intent = new Intent();
        intent.setClass(context, LoginActivity.class);
        intent.putExtra("drawback", true);
        context.startActivity(intent);
    }

    public static void startMainActivity(Activity activity) {
        Intent intent = new Intent();
        intent.setClass(activity, MainActivity.class);
        activity.startActivity(intent);
        activity.finish();
    }

    public static String getLanguageName() {
        if (I_Share.getLanguageType() == MyApplication.getInstance().getResources().getInteger(R.integer.language_simple)) {
            return MyApplication.getInstance().getResources().getString(R.string.lan_zh_cn);
        } else if (I_Share.getLanguageType() == MyApplication.getInstance().getResources().getInteger(R.integer.language_traditional)) {
            return MyApplication.getInstance().getResources().getString(R.string.lan_zh_tw);
        } else if (I_Share.getLanguageType() == MyApplication.getInstance().getResources().getInteger(R.integer.language_english)) {
            return MyApplication.getInstance().getResources().getString(R.string.lan_en_us);
        } else if (I_Share.getLanguageType() == MyApplication.getInstance().getResources().getInteger(R.integer.language_japanese)) {
            return MyApplication.getInstance().getResources().getString(R.string.lan_ja_jp);
        }
        return MyApplication.getInstance().getResources().getString(R.string.lan_zh_cn);

    }
    public static void post(Activity activity, String url, Map<String, Object> postParams, IHttpPostListener postListener, boolean showLoading, boolean useRSA){
        Ipost.send(activity,URLs.BASE_URL,url,postParams,postListener,showLoading,useRSA);
    }

}
